<?php
//文章控制器
	namespace Application\Admin\Controller;
	use Application\Admin\Model\Article as ArticleModel;
	use Application\Admin\Model\Articlecat;
	use KJ\Page;
	use KJ\Upload;
	class Article extends Admin{

		public function index(){
			
			$cat = new Articlecat();
			
			$this->cat = $cat -> select();
			
			$article = new ArticleModel();	
			
			$this->userKeyWord = '';	
			
			$data = $article->order('ctime desc')->select();
			
			foreach($data as $k => $val){
				
				$cat_id = $val['article_class'];
				
				$cat_name = $cat->where("id = $cat_id")->field('name')->select();
				
				$cat_name = $cat_name[0]['name'];
				
				$data[$k]['cat_name'] = $cat_name;
			}
			
									
			$count = count($data);
			
			$page = new Page($data, $count, 10);
			
			$data = $page->pagination();
			
			$this->data = $data;
			
			$this->render();
					
		}
		
		public function add(){			
			if(IS_POST){
				$article = new ArticleModel();
				if($_FILES['image']['size'] != 0){
					$upload = new Upload();
					$result = $upload -> upload('image');
					if(!empty($result['error'])){
						echo "<script>alert('上传信息有误');window.history.go(-1);</script>";
						exit;
					}else{
						$article -> attributes -> img_src = $result['realPath'];
					}
				}
				$article -> attributes -> img_name = $_POST['title'];
				$article -> attributes -> article_class = $_POST['cat_id'];
				$article -> attributes -> article = $_POST['content'];
				$article -> attributes -> ctime = time();
				
				if($article -> save()){
					echo "<script>alert('添加文章成功');window.location.href='index.php?m=admin&c=article&a=index';</script>";
					exit;
				}
				
			}

			$catModel = new Articlecat();
			$this->cat = $catModel->select();
			$this->render('addarticle');
			
		}		
		
		public function edit(){
			if(IS_GET){
				$id = $_GET['id'];
				$articleModel = new ArticleModel();
				
				$this->article = $articleModel -> find($id);
				
				$catModel = new Articlecat();
				
				$this->cat = $catModel->select();
				
				$this->render('editarticle');
			}
			if(IS_POST){
				$id = $_POST['id'];
				$articleModel = new ArticleModel();
				$article = $articleModel -> findModel($id);
				if($_FILES['image']['size'] != 0){
					$upload = new Upload();
					$upload -> EXTS = array('jpeg','jpg','png');
					$result = $upload -> upload('image');
					if(!empty($result['error'])){
						echo "<script>alert('上传信息有误');window.history.go(-1);</script>";
						exit;
					}else{
						$article -> attributes -> img_src = $result['realPath'];
					}
				}
				$article -> attributes -> img_name = $_POST['title'];
				$article -> attributes -> article_class = $_POST['cat_id'];
				$article -> attributes -> article = $_POST['content'];
				$article -> attributes -> ctime = time();
				if($article -> save()){
					echo "<script>alert('编辑成功');window.location.href='index.php?m=admin&c=article&a=index';</script>";
					exit;
				}
			}
			
		}		
		
		public function delete(){
			if(IS_GET){
				$id = $_GET['id'];
				$articleModel = new ArticleModel();
				$article = $articleModel ->  findModel($id);
				$src = $articleModel -> field('img_src') -> where("id = $id") -> select();
				$src = $src[0]['img_src'];
				if($article -> delete()){
					if(is_file($src)){
						unlink($src);
					}
					echo "<script>alert('删除成功');window.location.href='index.php?m=admin&c=article&a=index';</script>";
				}else{
					echo "<script>alert('删除失败');window.location.href='index.php?m=admin&c=article&a=index';</script>";
				}
			}
		}					
		
		public function select(){

			if(IS_POST){
				$keyword = $_POST['keyword'];
//				var_dump($keyword);exit;
				$_SESSION['userKeyWord'] = $keyword;
				$keyword = !isset($_POST['keyword']) ? '' : "and img_name like '%$keyword%'";
				$_SESSION['cat_id'] = $_POST['cat_id'];
//				var_dump($_SESSION['cat_id']);exit;
				$_SESSION['keyword'] = $keyword;
				$cat_id = $_POST['cat_id'];
				$userKeyWord = $_POST['keyword'];
			}
			if(IS_GET){
				$cat_id = $_SESSION['cat_id'];
				$keyword = $_SESSION['keyword'];
				$userKeyWord = $_SESSION['userKeyWord'];
			}
			$this->cat_id = $cat_id;
			
			$this->keyword = $keyword;
			
			$this->userKeyWord = $userKeyWord;
			
			$cat = new Articlecat();
			
			$this->cat = $cat -> select();
			
			$articleModel = new ArticleModel();
			
			if($cat_id == 0){
				$keyword = substr($keyword, 4);
				$article = $articleModel ->where("$keyword") -> order('ctime desc') -> select();
			}else{
				$article = $articleModel -> where("article_class = $cat_id $keyword") -> order('ctime desc')->select();
			}
			
			foreach($article as $k => $val){
				
				$cat_id = $val['article_class'];
				
				$cat_name = $cat->where("id = $cat_id")->field('name')->select();
				
				$cat_name = $cat_name[0]['name'];
				
				$article[$k]['cat_name'] = $cat_name;
			}
			
			$count = count($article);
		
			$page = new Page($article, $count, 10);
			
			$data = $page->pagination();
			
			$this->data = $data;
			
			$this->render('index');
		
		}		
		
	}
?>